package com.lebang.controller.buss;

import com.lebang.common.Login;
import com.lebang.entity.member.LbbMemberTrade;
import com.lebang.model.enums.buss.TaskStatus;
import com.lebang.model.enums.buss.TradeStatus;
import com.lebang.model.vo.buss.AppliesSearchVo;
import com.lebang.service.buss.MemberTradeService;
import com.lebang.service.sys.DictService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import static com.lebang.model.enums.buss.TradeStatus.Empty;

@Login
@Controller
public class AppliesController {
    @Autowired
    private DictService dictService;

    @Autowired
    private MemberTradeService memberTradeService;

    @Login
    @RequestMapping(value = {"/applies/list"}, method = {RequestMethod.GET})
    public String appliers(Model model) {
        model.addAttribute("cash_str",dictService.selectByKeyName("cash_msg").getKeyvalue());
        return "/applies/check-list";
    }

//    @Login
//    @RequestMapping(value = {"/applies/getList"}, method = {RequestMethod.POST})
//    @ResponseBody
//    public Map<String, Object> list(AppliesSearchVo vo, LbbMemberTrade trade) {
//        Map<String, Object> data = memberTradeService.appliesList(vo, trade);
//        return data;
//    }

    @Login
    @RequestMapping(value = {"/applies/pass"}, method = {RequestMethod.POST})
    @ResponseBody
    public JsonData pass(Long id, HttpSession session) {
        return memberTradeService.updateStatus(id, TradeStatus.Success, "", session.getAttribute("userid").toString());
    }

    @Login
    @RequestMapping(value = {"/applies/repulse"}, method = {RequestMethod.POST})
    @ResponseBody
    public JsonData repulse(Long id, @RequestParam("remark") String remark, HttpSession session) {
        return memberTradeService.updateStatus(id, TradeStatus.Invalid, remark, session.getAttribute("userid").toString());
    }

    @Login
    @RequestMapping(value = {"/applies/historyList"}, method = {RequestMethod.GET})
    public String history(Model model){
    model.addAttribute("cash_str",dictService.selectByKeyName("cash_msg").getKeyvalue());
        return "/applies/history_list";
    }

    @Login
    @RequestMapping(value = {"/applies/getHistoryList"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> historyList(AppliesSearchVo vo) {
        Map<String, Object> data = memberTradeService.historyList(vo);
        return data;
    }

    @Login
    @RequestMapping(value = "/applies/checkList", method = {RequestMethod.GET})
    public String checkList(Model model) {
        model.addAttribute("taskStatus", TaskStatus.values());
        model.addAttribute("cash_str",dictService.selectByKeyName("cash_msg").getKeyvalue());
        return "/applies/checkList";
    }


    @Login
    @RequestMapping(value = {"/applies/staff-list"}, method = {RequestMethod.GET})
    public String slaphappiest(Model model)
    {
        model.addAttribute("cash_str",dictService.selectByKeyName("cash_msg").getKeyvalue());
        return "/applies/staff-list";
    }

    @Login
    @RequestMapping(value = {"/applies/all-list"}, method = {RequestMethod.GET})
    public String alkalies(Model model) {
        model.addAttribute("cash_str",dictService.selectByKeyName("cash_msg").getKeyvalue());
        return "/applies/all-list";
    }

//    @Login
//    @RequestMapping(value = {"/applies/staff-getList"}, method = {RequestMethod.POST})
//    @ResponseBody
//    public Map<String, Object> snaffles(AppliesSearchVo vo, LbbMemberTrade trade, Integer page, Integer limit, String phone) {
//        Integer startNum = (page - 1) * limit;
//        vo.setOffset(startNum);
//        Map<String, Object> data = memberTradeService.staffappliesList(vo, trade, phone);
//        return data;
//    }

    /**
     * 提现通过后转账给用户接口**
     * param
     * id   提现订单id
     * payType = 1 支付宝自动转账
     * payType = 2 人工转账
     * 3 微信
     */
    @Login
    @RequestMapping(value = {"/applies/staff-pay"}, method = {RequestMethod.POST})
    @ResponseBody
    public JsonData staffPay(HttpSession session, Integer payType, Long id, String mark) {
        try {
            if (payType == null || id == null) {
                return Callback.fail("2010", "转账失败", null);
            }
            int i = 0;
            LbbMemberTrade trade=memberTradeService.selectById(id);
            if(trade==null){
                return Callback.fail("2020", "订单不存在");
            }
            if (!trade.getStatus().equals(Empty)) {
                return Callback.fail("2030", "转账失败,订单状态已被改变", null);
            }
            if(TextUtil.isEmpty(trade.getTradeNo())){
                java.util.Date t = new Date();
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
               String tradeNo= df.format(t) + UUIDUtil.M();
                if(payType.equals(1)){
                    tradeNo="ZFB"+tradeNo;

                }else if(payType.equals(3)){
                    tradeNo="WX"+tradeNo;
                }
                trade.setTradeNo(tradeNo);
                memberTradeService.updateTrade(trade);
            }
            if (payType.equals(1)) {
                return memberTradeService.staffPayByZfb(id, session.getAttribute("userid").toString(), mark);
            } else if(payType.equals(3)){

                return memberTradeService.staffPayByWx(id,session.getAttribute("userid").toString(), mark);
            }else {
                i = memberTradeService.staffPayByartificial(id, session.getAttribute("userid").toString());
            }
            if (i > 0) {
                return Callback.success("2040", "转账成功", null);
            } else {
                return Callback.fail("2050", "转账失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Callback.fail("2060", "转账失败", e.getMessage());
        }

    }

}
